<?php

/* 主播等级 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class LevelanchorController extends AdminBaseController
{

    public function index()
    {
        $params = $this->request->get();

        $where = [];
        if (!empty($params['langId'])) {
            $where['lang_id'] = $params['langId'];
        }else {
            $where['lang_id'] = $this->recommendLanguageData[0]['id'];
        }

        $list = Db::name('level_anchor')
            ->where($where)
            ->order("level asc")
            ->paginate(20);
        $list->appends($params);

        $list->each(function($v, $k){
            $v['thumb']=get_upload_path($v['thumb']);
            return $v;
        });
        
        $page = $list->render();
        $this->assign("page", $page);
            
        $this->assign('list', $list);

        $this->assign('langData', $this->recommendLanguageData);

        if (!empty($params['langId'])) {
            $this->assign('langId', $params['langId']);
        }else {
            $this->assign('langId', 0);
        }
        $this->assign('defaultLangId', $this->recommendLanguageData[0]['id']);

        return $this->fetch();
    }

    public function add()
    {

        $this->assign('langData', $this->recommendLanguageData);

        return $this->fetch();
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->param();
            
            if($data['level'] == ''){
                $this->error('等级不能为空');
            }else{
                $where = [
                    'del_flag' => 0,
                    'lang_id' => $data['lang_id'],
                    'level' => $data['level'],
                ];

                $check = Db::name('level_anchor')->where($where)->find();
                if($check){
                    $this->error('等级不能重复');
                }
            }
			
			if($data['levelname'] == ''){
                $this->error('名称不能为空');
            }
            
            $level_up=$data['level'];
            $thumb=$data['thumb'];
            
            if($level_up==''){
                $this->error('请填写经验上限');
            }

            if($thumb==''){
                $this->error('请上传图标');
            }
            
            
            $id = Db::name('level_anchor')->insertGetId($_POST);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->resetcache();
            $this->success("添加成功！");
        }
    }

    public function edit()
    {
        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('level_anchor')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }
        
        $this->assign('data', $data);
        $this->assign('langData', $this->recommendLanguageData);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data      = $this->request->post();

            if($data['level'] == ''){
                $this->error('等级不能为空');
            }else{
                $where = [
                    'del_flag' => 0,
                    'id' => $data['id']
                ];
                $level = Db::name('level_anchor')->where($where)->find();
                $where = [
                    'del_flag' => 0,
                    'lang_id' => $level['lang_id'],
                    'level' => $data['level'],
                ];
                $check = Db::name('level_anchor')->where($where)->where('id != '.$data['id'])->find();
//                $check = Db::name('level_anchor')->where("level='{$data['level']}' and id !='{$data['id']}'")->find();
                if($check){
                    $this->error('等级不能重复');
                }
            }
			
			if($data['levelname'] == ''){
                $this->error('名称不能为空');
            }
            
            $level_up=$data['level'];
            $thumb=$data['thumb'];
            
            if($level_up==''){
                $this->error('请填写经验上限');
            }

            if($thumb==''){
                $this->error('请上传图标');
            }
            
            
            $rs = Db::name('level_anchor')->update($data);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->resetcache();
            $this->success("保存成功！");
        }
    }


    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        
        $rs = Db::name('level_anchor')->where("id={$id}")->delete();
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->resetcache();
        $this->success("删除成功！",url("levelanchor/index"));
    }


    protected function resetcache(){
        $key='level_anchor';

        $level=Db::name('level_anchor')
                ->order("level asc")
                ->select();
        foreach($level as $k=>$v){
            $v['thumb']=get_upload_path($v['thumb']);
            $level[$k]=$v;
        }
        if($level){
            setcaches($key,$level);
        }
    }
}